package commons.base.nio;

import java.io.IOException;
import java.nio.channels.spi.SelectorProvider;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AcceptWorkerGroup implements AutoCloseable{

	private AcceptWorker acceptWorker;
	private ExecutorService executorService = Executors.newSingleThreadExecutor();

	public AcceptWorkerGroup(SelectorProvider selectorProvider, IOWorkerGroup ioWorkerGroup) throws IOException{
		acceptWorker = new AcceptWorker(selectorProvider, ioWorkerGroup);
		
		executorService.submit(acceptWorker);
	}
	
	public AcceptWorker chooseWorker(){
		return acceptWorker;
	}

	@Override
	public void close() throws Exception {
		try {
			acceptWorker.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			executorService.shutdown();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
